package com.hrpc.utils.zookeeper;

import com.hrpc.Constant;
import com.hrpc.exceptions.ZookeeperException;
import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class ZookeeperUtil {
    /**
     * 使用默认配置创建zookeeper实例
     * @return
     */
    public static ZooKeeper createZookeeper(){
        String connectString=Constant.Default_ZK_NCET;
        Integer timeout=Constant.TIMEOUT;
        ZooKeeper zookeeper = createZookeeper(connectString, timeout);
        return zookeeper;
    }

    public static ZooKeeper createZookeeper(String connectString,Integer timeout){
        CountDownLatch countDownLatch=new CountDownLatch(1);
        try {
            //创建zookeeper实例，建立连接
            final ZooKeeper zooKeeper = new ZooKeeper(connectString, timeout, event -> {
                // 只有连接成功才放行
                if(event.getState() == Watcher.Event.KeeperState.SyncConnected){
                    System.out.println("客户端已经连接成功。");
                    countDownLatch.countDown();
                }
            });
            countDownLatch.await();
            return zooKeeper;
        } catch (IOException  | InterruptedException e) {
            throw new ZookeeperException();
        }

    }

    /**
     * 用来创建一个节点
     * @param zooKeeper zookeeper实例
     * @param zookeeperNode 节点
     * @param watcher watcher实例
     * @param createMode 节点的类型
     * @return 是否成功创建
     */
    public static Boolean createNode(ZooKeeper zooKeeper,ZookeeperNode zookeeperNode,Watcher watcher,CreateMode createMode){
        try {
            if(zooKeeper.exists(zookeeperNode.getNodePath(),watcher)==null){
                try {
                    zooKeeper.create(zookeeperNode.getNodePath(), null,
                            ZooDefs.Ids.OPEN_ACL_UNSAFE,
                            createMode);
                    return true;
                } catch (KeeperException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (KeeperException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    /**
     * 关闭zookeeper
     * @param zooKeeper
     */
    public static void close(ZooKeeper zooKeeper){
        try {
            zooKeeper.close();
        } catch (InterruptedException e) {
            throw new ZookeeperException();
        }
    }

    /**
     * 判断节点是否存在
     * @param zooKeeper zk实例
     * @param path 路径
     * @param watcher watcher实例
     * @return
     */
    public static Boolean exists(ZooKeeper zooKeeper,String path,Watcher watcher){
        try {
            if(zooKeeper.exists(path,watcher) != null){
                return true;
            }else {
                return false;
            }
        } catch (KeeperException | InterruptedException e) {
            throw new ZookeeperException();
        }
    }

    /**
     * 查询一个节点的子元素
     *
     * @param zooKeeper  zk实例
     * @param serverNode 服务节点
     * @return 子元素列表
     */
    public static List<String> getChildren(ZooKeeper zooKeeper, String serverNode, Watcher watcher) {
        try {
            return zooKeeper.getChildren(serverNode, watcher);
        } catch (KeeperException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
